

let username = localStorage.username
let sectionBoxEl = document.querySelector('.sectionBox')
let setBtn = document.querySelector('.setBtn')
let bgMsgEl = document.querySelector('.message')
try {
    void(async function getFav() {
        let userFav = await axios({
            method: 'post',
            url: 'http://localhost:9527/api/data/getUserFav',
            data: {
                "username": username
            }
        })
        let favData = userFav.data.data

        if(!favData[0]) {
            bgMsgEl.style.display = "block"
        }
        Object.keys(favData).forEach((e) => {

            let cartDes = favData[e].cartDes
            let cartPrice = favData[e].cartPrice
            let cartImg = favData[e].cartImg
            let cartName = favData[e].cartName

            let contentEl = document.createElement('li')
            contentEl.classList.add('content')
            contentEl.dataset.name = cartName
            contentEl.innerHTML = `
            <ul class="checkBox active3">
                <span class="check"></span>
            </ul>
            <ul class="goodsBox">
                <div class="goodsImg">
                    <img src="${cartImg}" alt="">
                </div>
                <div class="goodsText">
                    <h3>${cartDes}</h3>
                    <p>100+人收藏</p>
                    <p>￥<span>${cartPrice}<span></p>
                </div>
            </ul>
            `
            sectionBoxEl.append(contentEl)


            let checkBoxList = document.querySelectorAll('.checkBox')
            let checkBox = document.querySelectorAll('.checkBox')
            let checkList = document.querySelectorAll('.check')
            let contentList = document.querySelectorAll('.content')
            let deleteBtn = document.querySelector('.delete')
            let allCheck = document.querySelector('.allCheck')
            let footer = document.querySelector('footer')
            let idDel = false
            checkList = Array.from(checkList);
            contentList = Array.from(contentList);

            setBtn.onclick = function () {
                checkBox.forEach(function (el) {
                    el.classList.toggle('active3')
                    footer.style.display = 'block'
                })
                if(!idDel){
                    setBtn.textContent = "完成"
                    idDel = true;
                } else {
                    setBtn.textContent = "管理"
                    idDel = false;
                }
            }
            allCheck.onclick = function (e) {
                let target = e.target;
                allCheck.classList.toggle('active5');
                if (allCheck.classList.contains('active5')) {
                    checkList.forEach(function (el) {
                        el.classList.add('active4')
                    })
                } else {
                    checkList.forEach(function (el) {
                        el.classList.remove('active4')
                    })
                }
            }
            
            // 小圆点
            checkList.forEach(function (el, index) {
                el.index = index;
                // let target = el.target;
                // if (el.classList.contains('.active4')) {
                //     console.log(123);
                // }
                el.onclick = function () {
                    let idx = this.index;
                    let targetEl = checkList[idx];
                    targetEl.classList.toggle('active4');
                    let sb = checkList.every((item) => {
                        return item.classList.contains('active4')
                    })
                    if (sb) {
                        allCheck.classList.add('active5')
                    } else {
                        allCheck.classList.remove('active5')
                    }
                }
            })
            deleteBtn.onclick = async function () {
                setBtn.textContent = "管理"
                idDel = false;
                footer.style.display = 'none'
                checkBox.forEach(function (el) {
                    el.classList.add('active3')
                })
                let thisCartName 
                let num  = 0
                contentList.forEach(function (item, index) {

                    if (item.querySelector(".active4")) {
                        num++
                        item.remove();
                        thisCartName+=`,${item.dataset.name}`;
                    }
                    allCheck.classList.remove('active5')
                })
                console.log(contentList.length);
                let result = await axios({
                    method: 'post',
                    url: 'http://localhost:9527/api/data/delFav',
                    data: {
                        "cartName": thisCartName
                    }
                })

                let timer = setTimeout(() =>{
                    if(contentList.length == num) {
                        bgMsgEl.style.display = "block"
                    }
                })
            }

        })
        let contentList = document.querySelectorAll('.content')
    })()
} catch (error) {
    if (error.response.status == 401) {
        // 鉴权失败
        alert('用户未登录，请登录后进行访问')
    }
}






